<template>
  <div id="app">
    <table class="tb">
      <tr>
        <th>编号</th>
        <th>品牌名称</th>
        <th>创立时间</th>
        <th>操作</th>
      </tr>
      <!-- 循环渲染的元素tr -->
      <tr v-for="(item,index) in list" :key="item.id">
            <td>{{item.id}}</td>
            <td>{{item.name}}</td>
            <td>{{item.time}}</td>
            <td>
                <button @click="del(index)">立即删除</button>
            </td>
        </tr>
        <tr >
            <td>
              <!-- <input type="text" class="form-control" placeholder="编号" v-model="id" /> -->
            </td>
            <td>
              <input type="text" class="form-control" placeholder="名称" v-model.number="name" />
            </td>
            <td></td>
            <td>
                <button class="btn btn-primary" @click.prevent="addFn">添加</button>
            </td>
        </tr>
      <tr v-if="list.length === 0">
        <td colspan="4">没有数据咯~</td>
      </tr>
    </table>
  </div>
</template>W

<script>
export default {
  data () {
    return {
      list: [
        { id: 1, name: '奔驰', time: '2020-08-01' },
        { id: 2, name: '宝马', time: '2020-08-02' },
        { id: 3, name: '奥迪', time: '2020-08-03' }
      ],
      id: '',
      name: ''

    }
  },
  methods: {
    del (index) {
      // 删除按钮 - 得到索引, 删除数组里元素
      this.list.splice(index, 1)
    },
    addFn () {
      // 5. 判断是否为空
      if (this.name.length === 0) {
        alert('不能为空')
        return
      }
      // 3. 把值以对象形式-插入list
      this.list.push({
        // 当前数组最后一个对象的id+1作为新对象id值
        id: this.list[this.list.length - 1].id + 1,
        name: this.name,
        time: new Date().getFullYear() + '-' + (+new Date().getMonth() + 1) + '-' + (new Date().getDate() < 10 ? '0' + new Date().getDate() : new Date().getDate())
      })
    }
  }
}

</script>
<style>
#app {
  width: 600px;
  margin: 10px auto;
}

.tb {
  border-collapse: collapse;
  width: 100%;
}

.tb th {
  background-color: #0094ff;
  color: white;
}

.tb td,
.tb th {
  padding: 5px;
  border: 1px solid black;
  text-align: center;
}

.add {
  padding: 5px;
  border: 1px solid black;
  margin-bottom: 10px;
}
</style>
